#ifndef GAMEENGINE_H
#define GAMEENGINE_H

#include "IChessView.h"
#include "board.h"
#include "ChessGuiDefines.h"

#include "myfunctions.h"
#include <vector>
#include "chesspiece.h"
#include "b_pawn.h"
#include "b_rook.h"
#include "b_king.h"
#include "b_knight.h"
#include "b_bishop.h"
#include "b_queen.h"
#include "nothing.h"
#include "xmlhandler.h"
#include "cell.h"
#include "move.h"
#include "ai.h"


class GameEngine
{
public:
    // My Classes
    IChessView * myView;
    Board myBoard;
    XMLHandler myXML;

    //save Info
    std::string saveLocation;

    //Turn Info
    Color turn;
    bool gameStarted;

    //Previously Selected Cell'l info
    Cell previousCell;
    std::vector <Cell> possibleMoves;

    //computer stuff
    bool clicked;
    bool whiteIsComputer;
    bool blackIsComputer;
    bool moving;
    AI myAI;

    //Undo Stuff
    bool didPawnPromote;


    GameEngine();
    ~GameEngine();

    //Called by Controller
    void loadView(IChessView * view);
    void startNewGame(int gameArgs);
    void loadGame();
    void saveGame();
    void saveGameAs();
    void cellSelect(int row , int col);
    void onTimer();
    void onUndo();

    //Called by user
    int ChooseNewGameType();
    void changeTurn();
    void StartFreshGame();
    void AddOtherPieces();
    bool isAMove(int row , int col);
    void FindMoves(std::vector<Cell> &myMoves , ChessPiece * p);
    void HighlightCells(std::vector<Cell> &cells);
    void MakePossibleMoves(int row, int col);
    void SetPiecesToBoard();
    bool TurnCheck(int row, int col);

    void StartNormalGame();
    void StartAGame();
    void doRandomComputerMove();
    bool doSelectPiece(int row , int col);
    bool doEndGame();
    void RemoveFromPossible(std::vector<Cell> &moves, std::vector<Cell> &badCells);
    void CheckCheck(int row, int col);
    void CheckCheckmate(std::vector<Cell> possBlackMoves,
                        std::vector<Cell> possWhiteMoves);
    void CheckColors(bool blackCheck, bool whiteCheck);
    void AddToMoves(std::vector<Cell> & addTo, std::vector<Cell> addFrom);
    void cellSelectComp(int row, int col);
    void CheckPawnPromote(int row, int col);
    void PromotePawn(int row, int col, Color white, signed int pieceType);
    bool CheckIsCastle(int row, int col);
    void CheckCastlingMoves(int row , int col);
    void CheckEnPassantSetup(int previousRow, int row, int col);
    bool CheckIsEnPassant(int row, int col);
    Move makeMove(int prow, int pcol, int row, int col);
    void onDragStart(int row, int col);
};

#endif // GAMEENGINE_H
